package com.shark.erp.controller;

import com.alibaba.fastjson.JSONObject;
import com.shark.erp.entity.Purchase;
import com.shark.erp.entity.PurchaseDetail;
import com.shark.erp.service.PurchaseDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class PurchaseDetailController {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @ResponseBody
    @GetMapping("/selectAllPurchaseDetail")
    public JSONObject selectAllPurchaseDetail(String manifestNumber) {
        return purchaseDetailService.selectAllPurchaseDetail(manifestNumber);
    }

    @ResponseBody
    @GetMapping("/selectPurchaseDetailByPurchaseDetailNumber")
    public JSONObject selectPurchaseDetailByPurchaseDetailNumber(@RequestParam("name") String name,@RequestParam("manifestNumber") String manifestNumber) {
        return purchaseDetailService.selectPurchaseDetailByPurchaseDetailNumber(name,manifestNumber);
    }

    @ResponseBody
    @PostMapping("/insertPurchaseDetail") //获取前端传来的json参数
    public JSONObject insertPurchaseDetail(@RequestBody PurchaseDetail purchaseDetail) {
        return purchaseDetailService.insertPurchaseDetail(purchaseDetail);
    }

    @ResponseBody
    @DeleteMapping("/deletePurchaseDetail")
    public JSONObject deletePurchaseDetail(@RequestParam("purchaseDetailNumber") String purchaseDetailNumber) {
        return purchaseDetailService.deletePurchaseDetail(purchaseDetailNumber);
    }

    @ResponseBody
    @PostMapping("/updatePurchaseDetail")
    public JSONObject updatePurchaseDetail(@RequestBody PurchaseDetail purchaseDetail) {
        return purchaseDetailService.updatePurchaseDetail(purchaseDetail);
    }

    /**
     * 区分该表单请求是添加还是修改
     */
    @PostMapping("/purchaseDetailAction")
    public String purchaseDetailAction(@RequestParam("action") String action) {
        if (action.equals("edit")) {
            return "forward:/updatePurchaseDetail";
        } else if (action.equals("add")) {
            return "forward:/insertPurchaseDetail";
        }
        return "404";
    }
}
